package edu.mercer.sse.ldap;

import java.security.GeneralSecurityException;
import java.sql.SQLException;
import java.util.Map;

import com.google.common.base.Function;
import com.unboundid.ldap.sdk.LDAPException;
import com.unboundid.ldif.LDIFException;

import edu.mercer.sse.annotations.Fulfills;
import edu.mercer.sse.errors.CriticalException;
import edu.mercer.sse.errors.ErrorLoggingVisitor;

/**
 * 
 * Update entry in LDAP.<p>
 * 
 * <ul>
 * <li> Modify attribute values.
 * <li> Add any missing attributes.
 * <li> Remove any attributes not present.
 * </ul>
 *
 * @see LDAPRevisePredicate
 * 
 * @see Function
 */

public class LDAPReviseFunction implements 
	
		Function<Map<String, String>, Boolean>
{
	/**
	 * No-arg constructor.<p>
	 * 
	 * Update entry in LDAP.<p>
	 * 
	 * <ul>
	 * <li> Modify attribute values.
	 * <li> Add any missing attributes.
	 * <li> Remove any attributes not present.
	 * </ul>
	 *
	 * @see LDAPRevisePredicate
	 * 
	 * @see Function
	 */
	
	public LDAPReviseFunction(){}
	
	
	@Override
	public Boolean apply( final Map<String, String> dbEntry )
	{
		Boolean result = false;
		
		try
		{
			result = LDAPUtility.writeEntry(
					
					LDAPUtility.findDN(dbEntry), 
					
					LDAPUtility.convertDBEntryToLDAP(dbEntry), 
					
					"overwrite");
		
		}
		catch( final SQLException             | 
				 	 LDAPException            | 
				 	 GeneralSecurityException |
				 	 LDIFException err )
		{
			final CriticalException errWrapped = new CriticalException( err );
			errWrapped.accept( new ErrorLoggingVisitor() );
			
			result = false;

		}//try

		return result;
	
	}//method
	

	@Fulfills("Object")
	@Override
	public String toString()
	{
		return "Revise";
		
	}//method
	
	
}//class
